<template>
    <div class="bgc11 w100vw h100vh pos-r">
      <dl class="mb10 bgc14">
        <dt class="item-title bdb11-1px">收货地址</dt>
        <dd class="flx-bm item-item bdb11-1px">
          <div>收货人：{{allInfo.usrData.address[0].name}}</div>
          <div>{{allInfo.usrData.address[0].number}}</div>
        </dd>
        <dd class="item-item bdb11-1px">
          {{allInfo.usrData.address[0].address}}
        </dd>
      </dl>
      <dl class="bgc14 mb10">
        <dt class="item-title bdb11-1px">豆瓣豆品</dt>
        <dd class="flx-bt item-item bdb11-1px" v-for="(i, n) in allInfo.billData.skuData" :key="n">
          <div>
            <img :src="i.image" class="w80 h80" alt="">
          </div>
          <div class="fz14">
            {{i.title}}
          </div>
          <div class="fz14 txt-r">
            <p>￥{{i.price}}</p>
            <p class="fz10 clr7"> x{{count(i.id)}} </p>
          </div>
        </dd>
      </dl>
      <dl class="bgc14">
        <dd class="item-item flx-bm bd11-1px">
          <span>商品总额：</span>
          <span>￥{{totalPrice()}}</span>
        </dd>
      </dl>
      <div class="pos-f b0 l0 h50 bgc14 w100p flx-rm">
        <div class="mr10">
          应付：
          <span class="clr2">￥{{totalPrice()}}</span>
        </div>
        <div class="w130 h100p bgc2 clr14 fz16 flx-mm">
          <span @click="createBill">提交订单</span>
        </div>
      </div>
    </div>
</template>

<script>
  import axios from 'axios'
  import {Toast} from 'vant'
  import {myTools} from "../assets/myTools/jsTools/jsTools"

  export default {
    async asyncData({app, query}) {
      let obj = {}
      obj.query = query
      let {status, data} = await app.$axiosApi.postUsrData({
          uid: query.uid
      })
      if(status === 200 && data.code === 0) {
        obj.usrData = data.data
      }else{
        console.log('checkBill asyncData get usr data error')
      }

      if(query.from === "product") {
        let billInfo = {}
        let {status, data} = await app.$axiosApi.postUsrCartSkuDetail({
            skuid: query.sid
        })
        if(status === 200 && data.code === 0) {
          billInfo.skuData = [data.data]
          billInfo.skuCount = [{sku_id: query.sid, itemCount: query.count}]
          obj.billData = billInfo
        }else {
          console.log('checkBill asyncData get sku data error from product')
        }
      }else if(query.from === 'cart') {
        let billInfo = {}
        let skuids = []
        obj.usrData.cart.forEach(i => {
          skuids.push(i.sku_id)
        })

        let {status, data} = await app.$axiosApi.postUsrCartSkusDetails({
            skuids: skuids
        })
        if(status === 200 && data.code === 0) {
          billInfo.skuData = data.data
          billInfo.skuCount = obj.usrData.cart
          obj.billData = billInfo
        }else {
          console.log('checkBill asyncData get skus datas error from product')
        }
      }
      return {
        allInfo: obj
      }
    },
    created() {
      //console.log(this.allInfo)
    },
    methods: {
      count(skuid) {
        let count
        this.allInfo.billData.skuCount.forEach(i => {
          if(i.sku_id === skuid) {
            count = i.itemCount
          }
        })
        return count
      },
      totalPrice(){
        let totalPrice = 0
        this.allInfo.billData.skuCount.forEach(i => {
          let price
          this.allInfo.billData.skuData.forEach(j => {
            if(i.sku_id === j.id) {
              price = j.price
            }
          })
          totalPrice = totalPrice + (i.itemCount * price)
        })
        return totalPrice
      },
      totalCount() {
        let totalCount = 0
        this.allInfo.billData.skuCount.forEach(i => {
          totalCount = totalCount + i.itemCount
        })
        return totalCount
      },
      async createBill(){
        let pros = []
        this.allInfo.billData.skuData.forEach(i=>{
          let obj = {
            "sku_id": i.id,
            "img": i.image,
            "title": i.title,
            "price": i.price,
            "count": this.allInfo.billData.skuCount[myTools.arrSomeItem(this.allInfo.billData.skuCount, 'sku_id', i.id)].itemCount,
            "color": i.color
          }
          pros.push(obj)
        })
        let bill = {
          "bill_id": "2756875043",
          "status": "unpaid",
          "products": pros,
          "total_count": this.totalCount(),
          "total_price": this.totalPrice(),
          "buyermsg": this.allInfo.usrData.address[0],
          "billmsg": {
            "billtype": "电子发票",
            "billtitle": "个人-张攀",
            "billemail": "676029339@qq.com",
            "billcontent": "豆瓣收藏夹"
          },
          "billdatemsg": {
            "bill_create_time": "2019-09-02 16:49:38",
            "bill_checked_time": "2019-09-02 16:49:38",
            "bill_dispatched_time": "2019-09-02 16:49:38",
            "bill_closed_time": "2019-09-02 16:49:38"
          }
        }
        if(this.allInfo.query.from === 'product') {
          let {status, data} = await this.$axiosApi.postCheckbillAddbill({
              uid: myTools.getSession('usr').uid,
              bill: bill,
              from: 'product'
          })
          if(status === 200 && data.code === 0) {
            Toast('已提交订单')
            window.location = '/bill'
          }
        }else{
          let {status, data} = await this.$axiosApi.postCheckbillAddbill({
              uid: myTools.getSession('usr').uid,
              bill: bill,
              from: 'cart'
          })
          if(status === 200 && data.code === 0) {
            Toast('已提交订单')
            window.location = '/bill'
          }
        }
      }
    }
  }
</script>

<style scoped lang="stylus">
.item-title
  font-weight bold;
  padding 15px;
.item-item
  padding 10px 15px;
  font-size 12px
</style>
